using System.Collections;
using UnityEngine;

public class Enemy9 : TypeOfEnemy
{
	public void Init(int arrayNum, int kindOfShield)
	{
		arrayNumForAllObjectsInfo = arrayNum;
		SetEnemy();
		StopAllCoroutines();
		StartCoroutine(Motions());
		StartCoroutine(RefreshPosition());
		StartCoroutine(IE_Update());
		StartCoroutine(IE_DrawShadow());
		EquipGetOrDrop("Enemy", true, 2);
		SetShield(kindOfShield);
		ChangeCharacterAnimType(1);
	}

	private void SetShield(int kindOfShield)
	{
		SCRT_WeaponAbility.sr.sprite = Sprites.instance.armorSpriteList[kindOfShield];
		SCRT_WeaponAbility.defencePower = ((kindOfShield != 0) ? 150 : 10);
	}

	private IEnumerator Motions()
	{
		LookAtThePlayer();
		AppearJump();
		yield return new WaitForSeconds(2f);
		while (AllObjectsInfo.enemy[arrayNumForAllObjectsInfo].hp > 0f)
		{
			yield return new WaitForSeconds(0.1f);
			if (CheckAction())
			{
				continue;
			}
			if (freezingTime > 0f)
			{
				freezingTime -= 0.1f;
				continue;
			}
			LookAtThePlayer();
			if (PlayerInTheEnemyAttackZone(arrayNumForAllObjectsInfo) && Player.instance.hp > 0f && (SCRT_OnTheFloorSensor.onTheFloorFlag || tf.position.y > 3f) && Calc.IsVisibleCharacterOnScreen(tf.position))
			{
				kindOfMotion = 1;
				Attack();
			}
			else if (SCRT_OnTheFloorSensor.onTheFloorFlag)
			{
				if (Vector2.Distance(Player.instance.tf.position, AllObjectsInfo.enemy[arrayNumForAllObjectsInfo].pos) < Player.instance.hitSize + attackRange.x + 10f && Player.instance.tf.position.y > AllObjectsInfo.enemy[arrayNumForAllObjectsInfo].pos.y + 2f)
				{
					JumpToTheTarget();
				}
				else if (at.GetCurrentAnimatorStateInfo(0).IsName("StickMan_Idle") && Player.instance.hp > 0f)
				{
					at.CrossFade(running_NormalList[0], 0.1f);
				}
			}
			else if (at.GetCurrentAnimatorStateInfo(0).IsName("StickMan_Idle"))
			{
				currentOnTheFloorFlag = false;
				if (!attackingNow)
				{
					at.Play(jumping_NormalList[0], -1, 0f);
				}
			}
		}
	}

	public override void Damaged(Vector2 attackedPos, AttackInfo attackInfo)
	{
		if (hp <= 0f)
		{
			return;
		}
		SlowMotionControl(0f);
		if (TF_InHands[0].childCount > 0)
		{
			tf.localScale = new Vector3((!(tf.position.x > Player.instance.tf.position.x)) ? (0f - characterScale) : characterScale, characterScale, 1f);
			at.Play(guard_NormalList[0], -1, 0f);
			Vector3 vector = new Vector2(Random.Range(-2f, 2f), Random.Range(-0.5f, 3f));
			Generator.instance.SpawnObj(15, TF_InHands[1].position + vector);
			Generator.instance.SpawnObj(16, TF_InHands[1].position + vector);
			Audio.instance.sfx[15].Play();
			if (SCRT_WeaponAbility.defencePower < 0)
			{
				EquipGetOrDrop("Enemy", false);
			}
			else
			{
				SCRT_WeaponAbility.defencePower -= attackInfo.power;
			}
			return;
		}
		HideAttackZone();
		Generator.instance.SpawnObj(9, new Vector2(TF_CenterBone.position.x, TF_CenterBone.position.y));
		hp -= attackInfo.power;
		AllObjectsInfo.enemy[arrayNumForAllObjectsInfo].hp = hp;
		Cam.instance.Shake(0.1f, 0.3f);
		Audio.instance.PlayWeaponSFX(attackInfo.kindOfWeapon);
		tf.localScale = new Vector3((!(tf.position.x > Player.instance.tf.position.x)) ? (0f - characterScale) : characterScale, characterScale, 1f);
		ComboText.instance.AddCombo();
		if (hp <= 0f)
		{
			GM.instance.SlowMotion(0.02f, (float)attackInfo.power * 0.0002f + 0.1f);
			StartCoroutine(IE_Die(attackInfo.force));
		}
		else
		{
			rb.velocity = Vector2.zero;
			at.Play(damaged_NormalList[0], -1, 0f);
		}
	}

	private IEnumerator IE_Die(Vector2 forceVel)
	{
		COL_AttackZone.gameObject.SetActive(false);
		col.enabled = false;
		COL_HitBox.enabled = false;
		attackingNow = false;
		GO_Blood.SetActive(true);
		GO_Blood.transform.localScale = tf.localScale;
		yield return new WaitForSeconds(0.02f);
		at.Play("StickMan_Damaged", -1, 1f);
		EquipGetOrDrop("Enemy", false);
		AttackZoneColOn(false);
		ComboText.instance.AddScore(300);
		Audio.instance.sfx[Random.Range(5, 8)].Play();
		yield return new WaitForSeconds(0.02f);
		col.enabled = true;
		at.enabled = false;
		IKDynamicOn(true, forceVel);
		if (SR_InHands[0].sprite != null && SR_InHands[0].sprite.name.Contains("Grenade"))
		{
			Throwing(0, true);
		}
		StartCoroutine(IE_DeActive());
	}
}
